package com.mlblog_springboot.web.admin;

import com.mlblog_springboot.Entity.User;
import com.mlblog_springboot.service.UserService;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("/OP")
public class LoginController {

    @Autowired
    private UserService userService;
    @RequestMapping("")
    public String loginPage(){
        return "OP/login";
    }

    @RequestMapping("/login")
    public String login(@RequestParam String username ,
                        String password,
                        HttpSession session,
                        RedirectAttributes redirectAttributes){
        User user = userService.checkUser(username,password);
        if(user != null){
            user.setPassword(null);
            session.setAttribute("user",user);
            return "OP/index";
        }else {
            redirectAttributes.addFlashAttribute("message","用户名或密码错误o.O");
            return "redirect:/OP";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/OP";
    }
}
